package com.ruoyi.business.service.impl;

import java.io.FileNotFoundException;
import java.util.List;

import com.ruoyi.business.domain.DefinitionInfo;
import com.ruoyi.business.mapper.DefinitionInfoMapper;
import com.ruoyi.business.service.IProcessService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DefinitionDetailMapper;
import com.ruoyi.business.domain.DefinitionDetail;
import com.ruoyi.business.service.IDefinitionDetailService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 流程定义明细Service业务层处理
 * 
 * @author Gekor
 * @date 2021-07-19
 */
@Service
public class DefinitionDetailServiceImpl implements IDefinitionDetailService 
{
    @Autowired
    private DefinitionDetailMapper definitionDetailMapper;
    @Autowired
    private IProcessService processService;


    /**
     * 查询流程定义明细列表
     * 
     * @param definitionDetail 流程定义明细
     * @return 流程定义明细
     */
    @Override
    public List<DefinitionDetail> selectDefinitionDetailList(DefinitionDetail definitionDetail)
    {
        List<DefinitionDetail> definitionDetailList = definitionDetailMapper.selectDefinitionDetailList(definitionDetail);
        for (DefinitionDetail detail : definitionDetailList) {
            ProcessDefinition processDefinition = processService.selectProcessDefinitonByDeployId(detail.getDeploymentId());
            detail.getDefinitionInfo().setDefinitionKey(processDefinition.getKey());
            detail.getDefinitionInfo().setDescription(processDefinition.getDescription());
            detail.setVersion(processDefinition.getVersion());
        }
        return definitionDetailList;
    }


    /** 流程部署 */
    @Transactional
    @Override
    public void processDeploy(String filepath, Long definitionInfoId) throws FileNotFoundException {
        Deployment deployment = processService.deploy(filepath);
        DefinitionInfo info = new DefinitionInfo();
        info.setId(definitionInfoId);
        DefinitionDetail detail = new DefinitionDetail();
        detail.setDefinitionInfo(info);
        detail.setDeploymentId(deployment.getId());

        definitionDetailMapper.insertDefinitionDetail(detail);
    }
}
